<script setup>
import { reactive , onMounted } from 'vue';
import { updateSettings , getSettings } from '@/api';
import { useRouter , useRoute } from 'vue-router';
import { Dialog } from '@varlet/ui';
import '@varlet/ui/es/dialog/style/index.js';
import { Snackbar } from '@varlet/ui'
import '@varlet/ui/es/snackbar/style/index.js';
const router = useRouter();
const $route = useRoute();

const setMaps = reactive({
  wxid: '',
  friend: false,
  collect: false,
  collect_shield_group_state: false,
  friend_original_shield: false,
  friend_address_shield: false
})
// 返回
const goBack = () => {
  router.go(-1);
}
// 载入事件
onMounted(async () => {
  let wxid = $route.params.wxid ?? false;
  if (!wxid || wxid == undefined || wxid =='') {
    return Dialog({
      title: '获取失败！',
      message: '请返回设置？',
      onConfirm: () => {
        return router.go(-1);
      }
    })
  }
  let r = await getSettings(wxid);
  if (r.code != 200) {
    return Dialog({
      title: r.msg,
      message: '请返回设置？',
      onConfirm: () => {
        return router.go(-1);
      }
    })
  }
  console.log($route.params);
  setMaps.friend = r?.data?.cofig?.friend ?? false;
  setMaps.collect = r?.data?.cofig?.collect ?? false;
  setMaps.collect_shield_group_state = r?.data?.cofig?.collect_shield_group_state ?? false;
  setMaps.friend_original_shield = r?.data?.cofig?.friend_original_shield ?? false;
  setMaps.friend_address_shield = r?.data?.cofig?.friend_address_shield ?? false;
})
// 修改配置 
async function UpdateSet() {
  let wxid = $route.params.wxid ?? '';
  setMaps.wxid = wxid;
  let r = await updateSettings(wxid, setMaps);
  if (r.code != 200) {
    return Dialog({
      title: '修改失败！',
      message: r.msg,
    })
  }
  return Snackbar({
    content: '修改成功!',
    duration: 3000
  })
}
</script>
<template>
  <var-app-bar title="转发设置" color="#fff" text-color="#000" title-position="center">
    <template #left>
      <var-button
        round
        text
        color="transparent"
        text-color="#000"
        @click="goBack"
      >
        <var-icon name="chevron-left" :size="24" />
      </var-button>
    </template>
  </var-app-bar>
  <div style="padding: 10px;">
    <h4>转发类型设置</h4> <var-divider />
    <var-cell title="同步转发">
      <template #extra>
        <var-switch v-model="setMaps.friend" />
      </template>
    </var-cell>
    <h4>收藏转发设置</h4>
    <var-divider />
    <var-cell title="收藏转发">
      <template #extra>
        <var-switch v-model="setMaps.collect" />
      </template>
    </var-cell>
    <var-cell title="收藏转发屏蔽">
      <template #extra>
        <var-switch v-model="setMaps.collect_shield_group_state" />
      </template>
    </var-cell>
    <h4>转发全局设置</h4>
    <var-divider />
    <var-cell title="屏蔽原创者">
      <template #extra>
        <var-switch v-model="setMaps.friend_original_shield" />
      </template>
    </var-cell>
    <var-cell title="屏蔽地区">
      <template #extra>
        <var-switch v-model="setMaps.friend_address_shield" />
      </template>
    </var-cell>
  
    <div style="text-align: center;margin-top: 18px;">
      <var-button text outline type="primary" @click="UpdateSet">保存设置</var-button>
    </div>
  </div>
</template>